<?php

namespace system\core;

use \system\exceptions\UrlNotFoundException;

/**
 * Static route discovering class
 * 
 * Tries to resolve {@link Route} for given $_SERVER['REQUEST_URI']
 * if {@link Route} resolved returns it else {@link UrlNotFoundException}
 * will be thrown
 *
 * @see Route, UrlNotFoundException
 * @package system\core
 * @author Pavel Egorov {@link mailto:pavel@egorov.ru pavel@egorov.ru}
 * @version 0.0.1 $Revision: 63 $
 * @copyright Copyright (c) 2011, Pavel Egorov
 */
final class Router {

    /**
     * Tries to resolve {@link Route}. {@link UrlNotFoundException} will
     * be thrown if REQUEST_URI doesn't matchs any {@link Route}
     *
     * @return Route Mathched {@link Route}
     */
    public static final function getRoute() {
        if (($route = Router::resolveURL()) != null) {
            return $route;
        } else {
            throw new UrlNotFoundException($_SERVER['REQUEST_URI']);
        }
    }

    /**
     * @access private
     */
    private static final function resolveURL() {
        foreach ($GLOBALS['routes'] as $route) {
            if ($route->matchRoute($_SERVER['REQUEST_URI'])) {
                $route->setGETvars($_SERVER['REQUEST_URI']);
                return $route;
            }
        }
        return null;
    }

}